<template>
  <div id="detail">
    <!--导航栏-->
    <detail-nav-bar ref="nav" class="detail-nav" @titleClick="titleClick"/>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="2">
      <!--顶部轮播图-->
      <detail-swiper :swiper-list="topImages"/>
      <!--基础信息-->
      <detail-base-info :goods="goods"/>
      <!--店铺信息-->
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="detailImageLoad"/>
      <detail-param-info ref="param" :param-info="paramInfo" />
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <good-list ref="recommend" :goods="recommendInfo"></good-list>
    </scroll>
    <back-top @click.native="backClick" v-show="back_top"></back-top>
    <detail-bottom-bar @addCart="addToCart"/>
  </div>
</template>

<script>
  import Scroll from "../../components/common/scroll/Scroll";
  import GoodList from "../../components/content/goods/GoodList";
  import BackTop from "../../components/common/backtop/BackTop";
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/DetailBaseInfo";
  import DetailShopInfo from "./childComps/DetailShopInfo";
  import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import DetailBottomBar from "./childComps/DetailBottomBar";
  import {getDetailData, Goods, Shop, GoodsParam, getRecommend} from "../../network/detail";
  import {debounce} from "../../common/utils/utils";

  export default {
    name: "Detail",
    components: {
      BackTop,
      DetailSwiper,
      DetailNavBar,
      DetailBaseInfo,
      DetailShopInfo,
      Scroll,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,
      GoodList
    },
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommendInfo: [],
        back_top: false,
        themeHeight:[],
        getOffsetTops: null
      }
    },
    methods: {
      detailImageLoad() {
        this.$refs.scroll.refresh();
        this.getOffsetTops();
      },
      goBack() {
        this.$router.go(-1)
      },
      contentScroll(position) {
        this.back_top = position.y > -1000 ? false : true
        const positionY = -position.y
        if (positionY >= this.themeHeight[3]) {
          this.$refs.nav.currentIndex = 3
        } else if(positionY >= this.themeHeight[2] && positionY < this.themeHeight[3]) {
          this.$refs.nav.currentIndex = 2
        } else if (positionY >= this.themeHeight[1] && positionY < this.themeHeight[2]) {
          this.$refs.nav.currentIndex = 1
        } else if (positionY >= this.themeHeight[0] && positionY < this.themeHeight[1]){
          this.$refs.nav.currentIndex = 0
        }
      },
      backClick() {
        this.$refs.scroll.scrollBack(0,50,1500)
        this.$refs.nav.currentIndex = 0
      },
      titleClick(index) {
        this.$refs.scroll.scrollBack(0, -this.themeHeight[index],200)
      },
      addToCart() {
        // 1.获取购物车需要展示的信息
        const product = {};
        product.iid = this.iid
        product.image = this.topImages[0];
        product.title = this.goods.title;
        product.desc = this.goods.desc;
        product.price = this.goods.realPrice;
        // 2.添加到购物车里
        // this.$store.commit('addCart', product)
        this.$store.dispatch('addCart', product).then(res => {
          console.log(res)
        })
      }
    },
    created() {
      this.iid = this.$route.params.iid
      getDetailData(this.iid).then(res => {
        // 1. 获取顶部的轮播图片
        const data = res.result
        this.topImages = data.itemInfo.topImages
        // 2. 获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services);
        // 3. 获取店铺信息
        this.shop = new Shop(data.shopInfo);
        // 4. 保存商品的详细信息
        this.detailInfo = data.detailInfo;
        // 5.获取参数的信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
        // 6.获取评论的信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }
        this.getOffsetTops = debounce(() => {
          this.themeHeight = []
          this.themeHeight.push(0)
          this.themeHeight.push(this.$refs.param.$el.offsetTop)
          this.themeHeight.push(this.$refs.comment.$el.offsetTop)
          this.themeHeight.push(this.$refs.recommend.$el.offsetTop)
        },50)
      })
      getRecommend().then(res => {
        this.recommendInfo = res.data.list
      })
    },
    mounted() {
      const refresh = debounce(this.$refs.scroll.refresh,50)
      this.$bus.$on('imageLoad', () => {
        refresh()
      })
    },
    destroyed() {
      this.$bus.$off('imageLoad')
    }
  }
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }
  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }
  .content {
    height: calc(100% - 44px);
  }
</style>
